<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <ul>
            <li v-for="( item , index ) in goods">
                商品Id: {{ item.id }} -- 商品名称: {{ item.name }} --
                商品价格: {{ item .price.toFixed(2) }}
                商品数量: 
                <button @click="goodsDel( item , index )">减</button>
                {{ item.count }}
                <button @click="goodsAdd( item )">加</button>
                商品小计: {{ item.allPrice.toFixed(2) }}
            </li>
        </ul>

        <p>
            总数量: {{ allCount }}
        </p>
        <p>
            总价格: {{ allPrice }}
        </p>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>

    <script>
        new Vue ({
            el : "#app",
            data : {
                allPrice : 600 ,
                allCount : 3 ,
                goods : [
                    {
                        id : 11 ,
                        name : "华为" ,
                        price : 100 ,
                        count : 1 ,
                        allPrice : 100
                    },
                    {
                        id : 22 ,
                        name : "小米" ,
                        price : 200 ,
                        count : 1 ,
                        allPrice : 200
                    },
                    {
                        id : 33 ,
                        name : "苹果" ,
                        price : 300 ,
                        count : 1 ,
                        allPrice : 300
                    }
                ]
            },
            methods : {
                fn(){
                    this.allCount = this.goods.reduce((prev , next) => {
                        return prev + next.count
                    }, 0)

                    this.allPrice = this.goods.reduce((prev , next) => {
                        return prev + next.count * next.price
                    }, 0)
                },
                goodsAdd( item ){
                    item.count += 1
                    item.allPrice = item.count * item.price ;this.fn()
                },
                goodsDel( item , index ) {
                    if ( item.count > 1 ){
                        item.count -= 1
                    } else {
                        this.goods.splice( index , 1 )
                    }
                    item.allPrice = item.count * item.price ;
                    this.fn ()
                }
            }
        })
    </script>

</body>
</html>